/*    
    LogParser - Log File Parser
    Copyright (C) 2007  Alois Kraus
  
    This source code is published under the Microsoft Permissive License (Ms-PL)
    http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx
*/

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using EntLibContrib.Logging.Parser;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Logging.UnitTests.Parser
{
    [TestClass]
    public class FilterTests
    {
        /// <summary>
        /// Check if the default filter does not filter at all
        /// </summary>
        [TestMethod]
        public void NullFilter()
        {
            Filter filter = new Filter();
            LogEntry entry = new LogEntry();

            Assert.IsTrue(filter.Match(entry));

            entry.Message = null;
            Assert.IsTrue(filter.Match(entry));

            entry.TimeStamp = DateTime.MinValue;
            Assert.IsTrue(filter.Match(entry));

            entry.TimeStamp = DateTime.MaxValue;
            Assert.IsTrue(filter.Match(entry));
        }


        /// <summary>
        /// Check if the filter does correctly filter out a time slice
        /// </summary>
        [TestMethod]
        public void TimeFilter()
        {
            Filter filter = new Filter();
            LogEntry entry = new LogEntry();


            DateTime curTime = DateTime.Now;
            entry.TimeStamp = curTime;

            // Check upper bound range match
            filter.StopTime = curTime.AddMilliseconds(1);
            Assert.IsTrue(filter.Match(entry));

            filter.StopTime = curTime.AddMilliseconds(-2);
            Assert.IsFalse(filter.Match(entry));

            filter.StopTime = DateTime.MaxValue;
            filter.StartTime = curTime.AddMilliseconds(1);
            Assert.IsFalse(filter.Match(entry));

            filter.StartTime = curTime.AddMilliseconds(-2);
            Assert.IsTrue(filter.Match(entry));
        }

        const string TestMessage = "TestMessage";
        const string SubStringMessage = "message";

        const string NonMatchMessage = "nonMatchingmsg";

        string[] MatchingFilter = new string[] { SubStringMessage, TestMessage };
        string[] EmptyFilter = new string[] { };



        [TestMethod]
        public void MessageFilter()
        {
            Filter filter = new Filter();
            LogEntry entry = new LogEntry();

            Assert.IsTrue(filter.Match(entry));

            filter.Messages = EmptyFilter;
            Assert.IsTrue(filter.Match(entry));

            filter.Messages = MatchingFilter;
            Assert.IsFalse(filter.Match(entry));
            entry.Message = TestMessage;
            Assert.IsTrue(filter.Match(entry));

            entry.Message = SubStringMessage;
            Assert.IsTrue(filter.Match(entry));

            entry.Message = NonMatchMessage;
            Assert.IsFalse(filter.Match(entry));
        }
    }
}
